package chen.web.proxy;

import java.lang.reflect.Method;

/**
 * 
 * @deprecated
 * @author chen
 *
 */
public class Operation {

	public static enum OperationStatus{before, after, exception};
	
	private Method method = null;
	private Object[] params = null;
	private boolean lackOfParams = false;
	private OperationStatus status = null;
	private Object result = null;
	
	public Operation(Method method){
		this.method = method;
		lackOfParams = true;
	}
	
	public Operation(Method method, Object[] params){
		this.method = method;
		this.params = params;
	}
	
	public boolean isLackOfParams(){
		return lackOfParams;
	}
	
	public Method getMethod(){
		return method;
	}
	
	public Object[] getParams(){
		return params;
	}
	
	public boolean inScope(Class<?> c){
		Class<?> cl = method.getDeclaringClass();
		if(cl.equals(c)){
			return true;
		} 
		return false;
	}
	
	@SuppressWarnings("unchecked")
	public <T> T getSingleParam(Class<T> requiredType){
		T result = null;
		boolean flag = false;
		if(params == null){
			return null;
		}
		for(Object param : params){
			if(requiredType.isInstance(param)){
				if(!flag){
					result = (T)param;
					flag = true;	
				} else {
					throw new IllegalArgumentException("发现有多个同类型的参数，不能使用此方法");
				}
			}
		}
		return result;
	}

	void setStatus(OperationStatus status) {
		this.status = status;
	}

	public OperationStatus getStatus() {
		return status;
	}

	void setResult(Object result) {
		if(status != OperationStatus.after){
			throw new IllegalStateException();
		}
		this.result = result;
	}

	public Object getResult() {
		if(status != OperationStatus.after){
			throw new IllegalStateException();
		}
		return result;
	}

}
